package com.kmmlearn.findtime.android.ui.animation

import androidx.compose.animation.Crossfade
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun CrossfadeDemo() {
    var currentPage by remember { mutableStateOf("A") }
    Column {
        Crossfade(targetState = currentPage) {
            when (it) {
                "A" -> Text("page A", fontSize = 50.sp)
                "B" -> Text("page B", fontSize = 50.sp)
            }
        }
        Button(onClick = { currentPage = if (currentPage == "A") "B" else "A" }) {
            Text("Switch", fontSize = 15.sp)
        }
    }

}


@Composable
fun AnimateContentSizeDemo() {
    var expanded by remember { mutableStateOf(false) }
    Column(Modifier.padding(16.dp)) {
        Text("AnimateContentSizeDemo", fontSize = 20.sp)
        Button(onClick = { expanded = !expanded }) {
            Text(if (expanded) "Shrink" else "Expand")
        }
        Spacer(Modifier.height(20.dp))
        Box(Modifier.background(Color.LightGray).animateContentSize()) {
            Text(
                text = "如上代码所示，expend决定文本的最大行数，也就决定了Box的整体尺寸，正常情况下大小的变化会立即生效，但是为Box添加Modifieir.animatedContentSize后，文本大小的变化会使用动画过渡",
                fontSize = 20.sp,
                textAlign = TextAlign.Justify,
                modifier = Modifier.padding(16.dp),
                maxLines = if (expanded) Int.MAX_VALUE else 2
            )
        }
    }
}